package com.zhengxin.one.utils;

import com.zhengxin.one.controller.BillController;
import com.zhengxin.one.controller.PersonFoundController;
import com.zhengxin.one.entity.BillAccumulation;
import com.zhengxin.one.entity.BillSociety;
import com.zhengxin.one.mapper.PolicyAddrMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class JobUtil {

    private final Logger log = LoggerFactory.getLogger(JobUtil.class);

    @Autowired
    private PolicyAddrMapper policyAddrMapper;

    //更新补缴定时器
    @Scheduled(cron = "0 0 1 * * ?")
    public void upDataRepair(){
        log.info("定时任务执行");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate now = LocalDate.now();
        //社保
        List<BillSociety> allSo = policyAddrMapper.getAllSo();
        allSo.forEach(x->{
            String Jtime=null;
            String format = sdf.format(x.getJointime());
            String format2 = now.format(fmt);
            String substring = format.substring(0, 4);
            String substring2 = format2.substring(0, 4);
            if(!substring.equals(substring2)){
                Jtime="01";
            }else {
                Jtime=format.substring(5,7);
            }
            int j = Integer.parseInt(Jtime);
            String format1 = x.getKeep_end_time().format(fmt);
            String substring1 = format1.substring(5, 7);
            int s = Integer.parseInt(substring1);
            int h = s-j;
           if(h>0){
               int numberSo = policyAddrMapper.getNumberSo(x.getId_card(),substring2);
               if(numberSo<h){
                   //修改状态
                   policyAddrMapper.changeBase(x.getId_card());
               }
           }
        });
        //公积金
        List<BillAccumulation> allAc = policyAddrMapper.getAllAc();
        allAc.forEach(x->{
            String Jtime=null;
            String format = sdf.format(x.getJointime());
            String format2 = now.format(fmt);
            String substring = format.substring(0, 4);
            String substring2 = format2.substring(0, 4);
            if(!substring.equals(substring2)){
                Jtime="01";
            }else {
                Jtime=format.substring(5,7);
            }
            int j = Integer.parseInt(Jtime);
            String format1 = x.getKeep_end_time().format(fmt);
            String substring1 = format1.substring(5, 7);
            int s = Integer.parseInt(substring1);
            int h = s-j;
            if(h>0){
                int numberSo = policyAddrMapper.getNumberAc(x.getId_card(),substring2);
                if(numberSo<h){
                    //修改状态
                    policyAddrMapper.changeBase(x.getId_card());
                }
            }
        });
    }
    //减员定时器
    @Scheduled(cron = "0 0 1 * * ?")
    public void upData() throws ParseException {
       List<Map<String, String>> downC = policyAddrMapper.getDownC();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        downC.forEach(x->{
            Date utilDate = null;
            try {
                utilDate = sdf.parse(x.get("s_downtime"));
            } catch (Exception e) {
                e.getMessage();
            }
            long time = utilDate.getTime();
            long currentTime = System.currentTimeMillis();
            if(currentTime>time){
                //删除所以信息
                policyAddrMapper.deInfoa(x.get("id_card"));
                policyAddrMapper.deInfob(x.get("id_card"));
                if("1".equals(x.get("service_type"))){
                    policyAddrMapper.deInfoc(x.get("id_card"),1);
                }
                if("2".equals(x.get("service_type"))){
                    policyAddrMapper.deInfoc(x.get("id_card"),2);
                }
                if("3".equals(x.get("service_type"))){
                    policyAddrMapper.deInfoc(x.get("id_card"),3);
                }
            }
        });

    }

}
